package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.jmp.base.CardApi;
import com.jmp.base.dto.*;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.facade.CompanyCardFacadeService;
import one.stand.mapper.BusinessCardMapperPublicExt;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.service.BusinessCardService;
import one.stand.service.CompanyServiceService;
import one.stand.service.user.TokenService;
import one.stand.util.RequestContextHolder;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
public class CardController implements CardApi {

    @Autowired
    private BusinessCardService businessCardService;
    @Autowired
    private CompanyServiceService companyServiceService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private CompanyCardFacadeService companyCardFacadeService;
    @Autowired
    private BusinessCardMapperPublicExt businessCardMapperPublicExt;

    @Override
    public ResultModel<CardListVo> list(@RequestBody CardListDto request) {

        Integer status = Objects.nonNull(request.getStatus()) ? request.getStatus() : 1;
        String keyword = SqlUtil.like(request.getKeyword());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<BusinessCard> cards = businessCardMapperPublicExt.selectByCompanyId(request.getCompanyId(), status, keyword);
        PageResponse2<CardListVo.Card> cardPageResponse2 = ConvertUtils.page2(cards, card -> {
            return CardListVo.Card.builder()
                    .cardId(card.getCardId())
                    .userId(card.getUserId())
                    .logo(card.getLogo())
                    .name(card.getName())
                    .position(card.getPosition())
                    .phone(card.getPhone())
                    .time(card.getCreateTime())
                    .build();
        });
        CardListVo vo = CardListVo.builder()
                .cardPage(cardPageResponse2)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel save(@RequestBody CardSaveRequest request) {
        businessCardService.createCard(request);
        return ResultModel.success();
    }

    @Override
    public ResultModel applyAgree(@RequestBody CardApplyAgreeDto request) {
        BusinessCard card = new BusinessCard();
        card.setCardId(request.getCardId());
        card.setAuditState(BusinessCardService.AUDIT_STATE_AGREE);
        businessCardMapperPublicExt.updateByPrimaryKeySelective(card);

        BusinessCard cardSelect = businessCardMapperPublicExt.selectByPrimaryKey(request.getCardId());
        tokenService.remove(Lists.newArrayList(Long.valueOf(cardSelect.getUserId())));
        return ResultModel.success();
    }

    @Override
    public ResultModel applyRefuse(@RequestBody CardApplyRefuseDto request) {
        companyCardFacadeService.quitCompany(request.getCardId(), RequestContextHolder.getRequestContext().getUser().getCardId());
        return ResultModel.success();
    }

    @Override
    public ResultModel edit(@RequestBody CardEditDto request) {
        BusinessCardRequest cardRequest = new BusinessCardRequest();
        cardRequest.setCardId(request.getCardId());
        cardRequest.setLogo(request.getLogo());
        cardRequest.setName(request.getName());
        cardRequest.setPhone(request.getPhone());
        cardRequest.setPosition(request.getPosition());
        cardRequest.setPersonalWx(request.getPersonalWx());
        cardRequest.setWxCode(request.getWxCode());
        cardRequest.setEmail(request.getEmail());
        cardRequest.setSoundTime(request.getSoundTime());
        cardRequest.setSoundRecording(request.getSoundRecording());
        cardRequest.setPhoto(request.getPhotos());
        cardRequest.setBriefIntroduction(request.getBriefIntroduction());
        cardRequest.setStyle(request.getStyle());
        businessCardService.updateCard(cardRequest);
        return ResultModel.success();
    }

    @Override
    public ResultModel wxcodeUpdate(@RequestBody CardWxcodeUpdateRequest request) {
        businessCardService.updateCardWxcode(request.getCardId(), request.getWxCode());
        return ResultModel.success();
    }

    @Override
    public ResultModel<CardCheckResponse> check(@RequestBody CardCheckRequest request) {
        BusinessCard card = businessCardService.get(request.getCardId());
        Long days = companyServiceService.check(card.getCompanyId());
        CardCheckResponse response = CardCheckResponse.builder()
                .effective(days > 0)
                .days(days)
                .build();
        return ResultModel.success(response);
    }
}
